package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public PageResult getByPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> p=(Page<DishVO>) dishMapper.getByPage(dishPageQueryDTO);
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    public void insert(DishDTO dishDTO) {
        Dish dish=Dish.builder()
                .id(dishDTO.getId())
                .name(dishDTO.getName())
                .categoryId(dishDTO.getCategoryId())
                        .price(dishDTO.getPrice())
                .status(dishDTO.getStatus())
                .description(dishDTO.getDescription())
                .image(dishDTO.getImage())
                .build();
        dishMapper.insert(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(!CollectionUtils.isEmpty(flavors)){
            flavors.forEach(e->e.setDishId(dish.getId()));
        }
        dishFlavorMapper.insertBatch(flavors);
    }

    @Override
    public DishVO getById(Long id) {
        return dishMapper.getById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class},  propagation = Propagation.REQUIRED)
    public void update(DishDTO dishDTO) {
         Dish dish=Dish.builder()
                .id(dishDTO.getId())
                .name(dishDTO.getName())
                .categoryId(dishDTO.getCategoryId())
                .price(dishDTO.getPrice())
                .status(dishDTO.getStatus())
                .description(dishDTO.getDescription())
                .image(dishDTO.getImage())
                .build();
         dishMapper.update(dish);
         long i=dish.getId();
         dishFlavorMapper.deleteById(Arrays.asList(i));
         List<DishFlavor> flavors = dishDTO.getFlavors();
         if(!CollectionUtils.isEmpty(flavors)){
             flavors.forEach(e->e.setDishId(dish.getId()));
         }
         dishFlavorMapper.insertBatch(flavors);
    }

    @Override
    public void deleteById(List<Long> ids) {
        //查看菜品状态如果处于起售状态则不能删除
        for (Long id : ids) {
            DishVO dish = dishMapper.getById(id);
            if(dish.getStatus()==1){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE );
            }
            if(setmealDishMapper.countByDishId(id)==0){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }
        //如果当前菜品关联了套餐也不能删除
        dishMapper.deleteById(ids);
        dishFlavorMapper.deleteById(ids);
    }

    @Override
    public List<Dish> list(Long categoryId) {
        return dishMapper.list(categoryId);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        dishMapper.startOrStop(status,id, LocalDateTime.now(),BaseContext.getCurrentId());
    }
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.search(dish);
        List<DishVO> dishVOList = new ArrayList<>();
        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);
            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());
            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
